package controleur;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JFileChooser;

import model.Contact;
import model.FileManagement;
import model.GestionAddress;
import view.ContactForm;
import view.PrincipalWindow;

public class ActionContact implements ActionListener{
	
	private ContactForm cont;
	private FileManagement lectfich;
	private Contact contact;
	private String dir = "";
	
	public ActionContact(ContactForm cont){
		this.cont = cont;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
				lectfich = new FileManagement();

		        if (e.getSource() == cont.getJButtonValidate()) {
		            
		            contact = new Contact(cont.getJTextFieldFname().getText(),
								    		cont.getJTextFieldLname().getText(),
								    		cont.getJTextFieldGroup().getText(),
								    		cont.getJTextFieldAddressH().getText(),
								    		cont.getJTextFieldAddressC().getText(),
								    		cont.getJTextFieldPhoneH().getText(),
								    		cont.getJTextFieldPhoneM().getText(),
								    		cont.getJTextFieldPhoneP().getText(),
								    		cont.getJTextFieldEmailPers().getText(),
								    		cont.getJTextFieldEmailProf().getText(),
								    		cont.getJTextFieldWebPage().getText(),
								    		cont.getJTextFieldBrowseIMG().getText()
							    		);

		            GestionAddress gA = new GestionAddress();
		            ArrayList<String> curContact = gA.addContact(MainControleur.data, contact);
		            if (curContact != null)
		            {
		            	ArrayList<ArrayList<String>> newcontact= new ArrayList<ArrayList<String>>();
		            	newcontact.add(curContact);

		            	try {
							lectfich.ecritureFichier(MainControleur.outputFile, newcontact, MainControleur.exist);
							ContactForm.jframe1.setVisible(false);
							MainControleur.fen.contactInfo(MainControleur.data);
							MainControleur.fen.remove(PrincipalWindow.jLabel1);
							PrincipalWindow.jLabel1.setIcon(new javax.swing.ImageIcon("fondframe.jpg")); // NOI18N
							PrincipalWindow.jLabel1.setText("jLabel1");
					        MainControleur.fen.getContentPane().add(PrincipalWindow.jLabel1);
					        PrincipalWindow.jLabel1.setBounds(0, 0, 950, 665);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
		            } 
		        }
		        
		        if (e.getSource() == cont.getJButtonBrowseIMG())
		        {
		        	final JFileChooser fc = new JFileChooser();
		            int rval = fc.showOpenDialog(ContactForm.jframe1);
		            String str = "";
		           
		            System.out.println(rval);
		            
		            if (rval == JFileChooser.APPROVE_OPTION) {
		            	System.out.println(fc.getCurrentDirectory().toString());
		            	dir = fc.getCurrentDirectory().toString();
		            	str = dir+ '\\' +fc.getSelectedFile().getName();
		                cont.getJTextFieldBrowseIMG().setText(str);
		                
		              }
		              if (rval == JFileChooser.CANCEL_OPTION) {
		            	dir = "src\\contact.png";
		            	cont.getJTextFieldBrowseIMG().setText(dir);
		            	
		              }

		        }
	}

	/**
	 * @return the dir
	 */
	public String getDir() {
		return dir;
	}

	/**
	 * @param dir the dir to set
	 */
	public void setDir(String dir) {
		this.dir = dir;
	}
}
